package com.appkubes.mall.restful.application.pay.strategy.free.service;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.restful.application.order.order.entity.OrderStateEnum;
import com.appkubes.mall.restful.application.order.order.service.impl.OrderServiceImpl;
import com.appkubes.mall.restful.application.pay.constants.PayConstants;
import com.appkubes.mall.restful.application.pay.entity.dto.PayOrderDTO;
import com.appkubes.mall.restful.application.pay.service.PayService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: Rongjie
 * @Date: 2020/2/23
 * @Description:
 */
@Service
public class PayFreeServiceImpl implements PayService {

    @Resource
    OrderServiceImpl orderServiceImpl;

    @Override
    public PayConstants.PaymentType getPaymentType() {
        return PayConstants.PaymentType.FREE;
    }

    @Override
    public PayOrderDTO pay(String orderKey) {
        PayOrderDTO resultDTO = new PayOrderDTO();
        resultDTO.setOrderKey(orderKey);

        MallOrder nowOrder = orderServiceImpl.getById(orderKey);
        nowOrder.setStatus(OrderStateEnum.PAYSUCCESS.value());
        nowOrder.setPayState(1);
        nowOrder.setPayTime(DateUtils.getCurrentTimestamp());
        nowOrder.setUpdateTime(DateUtils.getCurrentTimestamp());
        orderServiceImpl.updateById(nowOrder);

        resultDTO.setPaymentType(PayConstants.PaymentType.FREE.name());
        return resultDTO;
    }
}
